package Q9;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Question9 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取当前磁头所在柱面号n和请求数量m
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        scanner.nextLine();  // Consume the newline character

        // 读取请求柱面号序列
        List<Integer> requests = new ArrayList<>();
        String[] requestStrings = scanner.nextLine().split(" ");
        for (String request : requestStrings) {
            requests.add(Integer.parseInt(request));
        }

        // 读取移动方向
        int direction = scanner.nextInt();

        // 调用调度方法获取响应序列
        List<Integer> responseSequence = elevatorScheduler(n, requests, direction);

        // 输出结果
        for (int i = 0; i < responseSequence.size(); i++) {
            System.out.print(responseSequence.get(i));
            if (i < responseSequence.size() - 1) {
                System.out.print(" ");
            }
        }
    }

    private static List<Integer> elevatorScheduler(int currentPosition, List<Integer> requests, int direction) {
        List<Integer> sortedRequests = new ArrayList<>(requests);
        Collections.sort(sortedRequests); // 排序请求序列

        List<Integer> responseSequence = new ArrayList<>();

        if (direction == 1) { // 向上移动
            for (int request : sortedRequests) {
                if (request >= currentPosition) {
                    responseSequence.add(request);
                }
            }
            for (int request : sortedRequests) {
                if (request < currentPosition) {
                    responseSequence.add(request);
                }
            }
        } else { // 向下移动
            for (int i = sortedRequests.size() - 1; i >= 0; i--) {
                int request = sortedRequests.get(i);
                if (request <= currentPosition) {
                    responseSequence.add(request);
                }
            }
            for (int request : sortedRequests) {
                if (request > currentPosition) {
                    responseSequence.add(request);
                }
            }
        }

        return responseSequence;
    }
}
